package com.goldcn.user.controller;

import com.goldcn.user.model.Message;
import com.goldcn.user.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1111 on 2016/12/6.
 */
@Api(value = "消息中心")
@RestController
@RequestMapping(value = "/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    @ApiOperation(value = "新增消息")
    @RequestMapping(method = RequestMethod.POST)
    public Object addMessage(@RequestBody Message message){
        return messageService.addMessage(message);
    }

    @ApiOperation(value = "查看所有消息列表")
    @RequestMapping(value = "/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Object selectAllMessage(@PathVariable int pageNum,@PathVariable int pageSize){
        return messageService.selectAllMessage(pageNum,pageSize);
    }

    @ApiOperation(value = "按类型消息列表[type:1pc端消息，2APP端消息]")
    @RequestMapping(value = "/{type}/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Object selectMessageByType(@PathVariable int type,@PathVariable int pageNum,@PathVariable int pageSize){
        return messageService.selectMessageByType(type,pageNum,pageSize);
    }

    @ApiOperation(value = "按userID查看消息列表")
    @RequestMapping(value = "/user/{userID}/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Object selectUserMessageByUserID(@PathVariable Long userID,@PathVariable int pageNum,@PathVariable int pageSize){
        return messageService.selectUserMessageByUserID(userID,pageNum,pageSize);
    }

    @ApiOperation(value = "按userID查看消息列表")
    @RequestMapping(value = "/web/user/{userID}/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Object selectEmployMsgForWeb(@PathVariable Long userID,@PathVariable int pageNum,@PathVariable int pageSize){
        return messageService.selectEmployMsgForWeb(userID,pageNum,pageSize);
    }

    @ApiOperation(value = "按消息id查询消息详情")
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public Object selectUserMessageByID(@PathVariable Long id){
        return messageService.selectUserMessageByID(id);
    }

    @ApiOperation(value = "按消息id删除消息")
    @RequestMapping(value = "/user/delete/{id}",method = RequestMethod.GET)
    public Object deleteUserMessageByID(@PathVariable Long id){
        return messageService.deleteUserMessageByID(id);
    }

    @ApiOperation(value = "按id修改消息状态为已读")
    @RequestMapping(value = "/user/update/{id}",method = RequestMethod.GET)
    public Object updateStateByID(@PathVariable Long id){
        return messageService.updateStateByID(id);
    }

    @ApiOperation(value = "批量修改消息状态为已读")
    @RequestMapping(value = "/user/update",method = RequestMethod.PUT)
    public Object updateState(@RequestBody List<Long> idList){
        for(Long id : idList) {
            messageService.updateStateByID(id);
        }
        return "success";
    }

    @ApiOperation(value = "按userID查询未读消息数量")
    @RequestMapping(value = "/user/qty/{userID}",method = RequestMethod.GET)
    public Object getNotReadMessageQty(@PathVariable Long userID){
        return messageService.getNotReadMessageQty(userID,false);
    }

    @ApiOperation(value = "按userID查询未读消息数量")
    @RequestMapping(value = "/web/user/qty/{userID}",method = RequestMethod.GET)
    public Object getNotReadMessageQtyForWeb(@PathVariable Long userID){
        return messageService.getNotReadMessageQty(userID,true);
    }
}
